<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="收费项分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择收费项分类" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.sys_fee_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="收费项目名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入收费项目名" clearable @change="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:fee_item:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:fee_item:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:fee_item:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:fee_item:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fee_itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="收费项分类" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_fee_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="收费项目名" align="center" prop="name" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="numType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_num_type" :value="scope.row.numType" />
        </template>
      </el-table-column>
      <el-table-column label="费用周期(月为单位)" align="center" prop="period">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_period_type" :value="scope.row.period" />
        </template>
      </el-table-column>
      <el-table-column label="费用计算公式" align="center" prop="formula" />
      <el-table-column label="是否产生滞纳金" align="center" prop="lateFeeEnable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_late_fee" :value="scope.row.lateFeeEnable" />
        </template>
      </el-table-column>
      <el-table-column label="滞纳金天数" align="center" prop="lateFeeDays" />
      <el-table-column label="滞纳金比例" align="center" prop="lateFeeRate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:fee_item:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:fee_item:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改收费项管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="收费项分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择收费项分类">
            <el-option v-for="dict in dict.type.sys_fee_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费项目名" prop="name">
          <el-input v-model="form.name" placeholder="请输入收费项目名" />
        </el-form-item>
        <el-form-item label="单价" prop="price" v-if="form.type === 'house'">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="数量" prop="numType" v-if="form.type === 'house'">
          <el-select v-model="form.numType" placeholder="请选择数量">
            <el-option v-for="dict in dict.type.sys_num_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用周期(月为单位)" prop="period" v-if="form.type === 'house'">
          <el-select v-model="form.period" placeholder="请选择费用周期(月为单位)">
            <el-option v-for="dict in dict.type.sys_period_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用计算公式" prop="formula" v-if="form.type === 'house'">
          <el-input v-model="form.formula" placeholder="单价*数量" name="单价*数量" value="单价*数量" disabled />
        </el-form-item>
        <el-form-item label="是否产生滞纳金" prop="lateFeeEnable" v-if="form.type === 'house'">
          <el-radio-group v-model="form.lateFeeEnable">
            <el-radio v-for="dict in dict.type.sys_late_fee" :key="dict.value"
              :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="滞纳金天数" prop="lateFeeDays" v-if="form.lateFeeEnable === 1">
          <el-input v-model="form.lateFeeDays" placeholder="请输入滞纳金天数" />
        </el-form-item>
        <el-form-item label="滞纳金比例" prop="lateFeeRate" v-if="form.lateFeeEnable === 1">
          <el-input v-model="form.lateFeeRate" placeholder="请输入滞纳金比例" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listFee_item,
    getFee_item,
    delFee_item,
    addFee_item,
    updateFee_item
  } from "@/api/system/fee_item";

  export default {
    name: "Fee_item",
    dicts: ['sys_late_fee', 'sys_num_type', 'sys_period_type', 'sys_fee_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 收费项管理表格数据
        fee_itemList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: null,
          name: null,
        },
        // 表单参数
        form: {
          formula: '单价*数量',
        },
        // 表单校验
        rules: {
          type: [{
            required: true,
            message: "收费项分类不能为空",
            trigger: "change"
          }],
          name: [{
            required: true,
            message: "收费项目名不能为空",
            trigger: "blur"
          }],
          price: [{
            required: true,
            message: "单价不能为空",
            trigger: "blur"
          }],
          numType: [{
            required: true,
            message: "数量不能为空",
            trigger: "change"
          }],
          period: [{
            required: true,
            message: "费用周期(月为单位)不能为空",
            trigger: "change"
          }],
          formula: [{
            required: true,
            message: "费用计算公式不能为空",
            trigger: "blur"
          }],
          lateFeeEnable: [{
            required: true,
            message: "是否产生滞纳金不能为空",
            trigger: "change"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询收费项管理列表 */
      getList() {
        this.loading = true;
        listFee_item(this.queryParams).then(response => {
          this.fee_itemList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          type: null,
          name: null,
          price: null,
          numType: null,
          period: null,
          formula: '单价*数量',
          formulaExpression: null,
          generateType: null,
          lateFeeEnable: null,
          lateFeeDays: null,
          lateFeeRate: null,
          creatorTime: null,
          creatorUserId: null,
          lastModifyUserId: null,
          lastModifyTime: null,
          deleteUserId: null,
          deleteTime: null,
          enabledMark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加收费项管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getFee_item(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改收费项管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateFee_item(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFee_item(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除收费项管理编号为"' + ids + '"的数据项？').then(function() {
          return delFee_item(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/fee_item/export', {
          ...this.queryParams
        }, `fee_item_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
